﻿namespace GSC.WebControls
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.ComponentModel.Design;
    using System.Web.UI.HtmlControls;
    /// <summary>
    /// 左边垂直菜单控件设计器。
    /// </summary>
    /// <remarks>左边垂直菜单控件设计器。</remarks>
    internal sealed class BackgroundCenterLeftButtonControlDesigner : DataBoundControlDesigner
    {
        BackgroundCenterLeftButtonControl _CenterLeftButton;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Component"></param>
        public override void Initialize(IComponent Component)
        {
            base.Initialize(Component);
            this._CenterLeftButton = (BackgroundCenterLeftButtonControl)Component;
        }
        protected override void PostFilterProperties(System.Collections.IDictionary properties)
        {
            base.PostFilterProperties(properties);
            if (string.IsNullOrEmpty(this.DataSourceID))
            {
                if (!properties.Contains("Buttons"))
                {
                    PropertyDescriptor pdItems = TypeDescriptor.CreateProperty(
                        typeof(BackgroundCenterLeftButtonControlDesigner),
                        "Buttons",
                        typeof(BackgroundCenterLeftMenuButtonControlCollection),
                        new Attribute[] { new DesignOnlyAttribute(true) });
                    properties.Add("Buttons", pdItems);
                }
            }
            else
            {
                if (properties.Contains("Buttons"))
                    properties.Remove("Buttons");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string GetDesignTimeHtml()
        {
            StringWriter sw = null;
            HtmlTextWriter tw = null;
            try
            {
                //
                if (GetDesignTimeDataSource() != null && !string.IsNullOrEmpty(this._CenterLeftButton.DataSourceID))
                {
                    _CenterLeftButton.Buttons.Clear();
                    //键值
                    string _DataTextField = _CenterLeftButton.DataTextField;
                    string _DataCommandArgumentField = _CenterLeftButton.DataCommandArgumentField;
                    string _DataCommandNameField = _CenterLeftButton.DataCommandNameField;
                    string _DataToolTipField = _CenterLeftButton.DataToolTipField;
                    string _DataPrimaryKeyField = _CenterLeftButton.DataValueField;
                    //
                    foreach (object dataItem in this.GetDesignTimeDataSource())
                    {
                        foreach (object cdataItem in this.GetDesignTimeDataSource())
                        {
                            BackgroundCenterLeftMenuButtonControl b = new BackgroundCenterLeftMenuButtonControl();
                            if (!string.IsNullOrEmpty(_DataTextField))
                            {
                                b.Text = DataBinder.GetPropertyValue(cdataItem, _DataTextField, null);
                            }
                            //
                            if (!string.IsNullOrEmpty(_DataCommandArgumentField))
                            {
                                b.CommandArgument = DataBinder.GetPropertyValue(cdataItem, _DataCommandArgumentField, null);
                            }
                            //
                            if (!string.IsNullOrEmpty(_DataCommandNameField))
                            {
                                b.CommandName = DataBinder.GetPropertyValue(cdataItem, _DataCommandNameField, null);
                            }
                            //
                            if (!string.IsNullOrEmpty(_DataToolTipField))
                            {
                                b.ToolTip = DataBinder.GetPropertyValue(cdataItem, _DataToolTipField, null);
                            }
                            this._CenterLeftButton.Buttons.Add(b);
                        }
                    }
                }
                ((ICompositeControlDesignerAccessor)this._CenterLeftButton).RecreateChildControls();
                foreach (BackgroundCenterLeftMenuButtonControl citem in _CenterLeftButton.Buttons)
                {
                    ((ICompositeControlDesignerAccessor)citem).RecreateChildControls();
                }
                if (_CenterLeftButton.Buttons.Count > 0)
                {
                    Panel panel = new Panel();
                    panel.Style["overflow"] = "hidden";
                    panel.Width = this._CenterLeftButton.Width;
                    panel.Height = this._CenterLeftButton.Height;
                    foreach (BackgroundCenterLeftMenuButtonControl citem in _CenterLeftButton.Buttons)
                    {
                        Panel buttons = new Panel();
                        buttons.Style["overflow"] = "hidden";
                        buttons.Style["background-position"] = "left top";
                        buttons.Style["white-space"] = "nowrap";
                        buttons.Style["text-overflow"] = "ellipsis";
                        buttons.Height = this._CenterLeftButton.ButtonHeight;
                        buttons.Width = this._CenterLeftButton.Width;
                        if (citem.IsSelected)
                            if (string.IsNullOrEmpty(this._CenterLeftButton.SelectedButtonBackImage))
                                buttons.Style["background-image"] = "url(" + this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.GetWebResourceUrl("SelectedButtonBg.gif")) + ")";
                            else
                                buttons.Style["background-image"] = "url(" + this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.SelectedButtonBackImage) + ")";
                        else
                            if (string.IsNullOrEmpty(this._CenterLeftButton.ButtonBackImage))
                                buttons.Style["background-image"] = "url(" + this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.GetWebResourceUrl("ButtonBg.gif")) + ")";
                            else
                                buttons.Style["background-image"] = "url(" + this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.ButtonBackImage) + ")";
                        panel.Controls.Add(buttons);
                        //
                        Panel buttonsContainer = new Panel();
                        buttonsContainer.Height = this._CenterLeftButton.ButtonHeight;
                        buttonsContainer.Width = this._CenterLeftButton.Width;
                        buttonsContainer.Style["overflow"] = "hidden";
                        buttonsContainer.Style["font-size"] = "0px";
                        buttonsContainer.Style["padding-top"] = "5px";
                        buttonsContainer.Style["padding-left"] = "4px";
                        buttons.Controls.Add(buttonsContainer);
                        //
                        LinkButton kinkButtons = new LinkButton();
                        kinkButtons.Text = citem.Text;
                        kinkButtons.ForeColor = citem.ForeColor;
                        kinkButtons.Font.CopyFrom(citem.Font);
                        buttonsContainer.Controls.Add(kinkButtons);
                    }
                    //
                    if (this._CenterLeftButton.ShowEnd)
                    {
                        Panel end = new Panel();
                        end.Style["background-position"] = "left top";
                        end.Width = this._CenterLeftButton.Width;
                        end.Height = this._CenterLeftButton.EndHeight;
                        end.Style["background-image"] = "url(" + this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.GetWebResourceUrl("EndBg.gif")) + ")";
                        panel.Controls.Add(end);
                    }
                    //
                    sw = new StringWriter();
                    tw = new HtmlTextWriter(sw);
                    panel.RenderControl(tw);
                    return sw.ToString();
                }
                else
                {
                    System.Resources.ResourceManager rm = new System.Resources.ResourceManager(typeof(Properties.Resources));
                    return "<div style='white-space:nowrap;text-overflow:ellipsis;overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + "; background-image:url(" + (this._CenterLeftButton.ButtonBackImage == string.Empty ? this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.GetWebResourceUrl("ButtonBg.gif")) : this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.ButtonBackImage)) + ");'>" +
                           "<div style='font-size: 12px; font-weight: bold;padding-top: 7px; padding-left: 4px; overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + ";'>" +
                           rm.GetString("Manage_CenterLeftMenu_Designer_DemoButton") +
                           "</div>" +
                           "</div>" +
                           "<div style='white-space:nowrap;text-overflow:ellipsis;overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + "; background-image:url(" + (this._CenterLeftButton.ButtonBackImage == string.Empty ? this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.GetWebResourceUrl("ButtonBg.gif")) : this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.ButtonBackImage)) + ");'>" +
                           "<div style='font-size: 12px; font-weight: bold;padding-top: 7px; padding-left: 4px; overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + ";'>" +
                           rm.GetString("Manage_CenterLeftMenu_Designer_DemoButton") +
                           "</div>" +
                           "</div>" +
                           "<div style='white-space:nowrap;text-overflow:ellipsis;overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + "; background-image:url(" + (this._CenterLeftButton.SelectedButtonBackImage == string.Empty ? this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.GetWebResourceUrl("SelectedButtonBg.gif")) : this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.SelectedButtonBackImage)) + ");'>" +
                           "<div style='font-size: 12px; font-weight: bold;padding-top: 7px; padding-left: 4px; overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + ";'>" +
                           rm.GetString("Manage_CenterLeftMenu_Designer_DemoSelectedButton") +
                           "</div>" +
                           "</div>" +
                           "<div style='white-space:nowrap;text-overflow:ellipsis;overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + "; background-image:url(" + (this._CenterLeftButton.ButtonBackImage == string.Empty ? this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.GetWebResourceUrl("ButtonBg.gif")) : this._CenterLeftButton.ResolveClientUrl(this._CenterLeftButton.ButtonBackImage)) + ");'>" +
                           "<div style='font-size: 12px; font-weight: bold;padding-top: 7px; padding-left: 4px; overflow:hidden ;width:" + this._CenterLeftButton.Width.ToString() + " ;height:" + this._CenterLeftButton.ButtonHeight.ToString() + ";'>" +
                           rm.GetString("Manage_CenterLeftMenu_Designer_DemoButton") +
                           "</div>" +
                           "</div>";
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public override void OnComponentChanging(object sender, ComponentChangingEventArgs ce)
        {
            base.OnComponentChanging(sender, ce);
        }
        public override void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
        {
            base.OnComponentChanged(sender, ce);
            /*
            switch (ce.Member.Name)
            {
                case "MultiLine":
                    if (!(bool)ce.NewValue)
                    {
                        bool ise = true;
                        foreach (CenterLeftButtonTitle item in this._LeftMenu.Titles)
                        {
                            if (ise && item.IsExpanded)
                            {
                                ise = false;
                            }
                            else
                            {
                                item.IsExpanded = false;
                            }
                        }
                    }
                    break;
            }
             */ 
        }
        /*
        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            for (int i = 0; i < this._LeftMenu.Titles.Count; i++)
            {
                DesignerRegion dr = new DesignerRegion(this, "Title|" + i.ToString());
                regions.Add(dr);
                for (int n = 0; n < this._LeftMenu.Titles[i].Buttons.Count; n++)
                {
                    dr = new DesignerRegion(this, "Button|" + n.ToString());
                    regions.Add(dr);
                }
            }
            return base.GetDesignTimeHtml(regions);
        }
        protected override void OnClick(DesignerRegionMouseEventArgs e)
        {
            int i = 0;
            string[] d = e.Region.Name.Split(new char[] { '|' });
            switch (d[0])
            {
                case "Title":

                    break;
                case "Button":
                    foreach (CenterLeftButtonTitle item in this._LeftMenu.Titles)
                    {
                        foreach (CenterLeftButtonButton citem in item.Buttons)
                        {
                            if (i.ToString().Equals(d[1]))
                            {
                                citem.IsSelected = true;
                            }
                            else
                            {
                                citem.IsSelected = false;
                            }
                            i++;
                        }
                    }
                    break;
            }
            base.OnClick(e);
        }
         */ 
    }
}